package net.oschina.app.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import net.oschina.app.R;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by admin on 2017/7/13.
 */

public abstract class BaseVPFragment extends BaseFragment {
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.vp_pager)
    ViewPager vpPager;
    Unbinder unbinder;
    private Context context;
    private BaseAdapter adapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //1. 初始化布局, 将包含页面滑动标签(PageSlidngTabStrip\TabLayout),ViewPager的布局初始化
        View view = View.inflate(context, R.layout.base_viewpager_layout, null);
        unbinder = ButterKnife.bind(this, view);
        ////2. 创建数据适配器   PagerAdapter
        adapter =new BaseAdapter(getChildFragmentManager(),context);
        //3设置适配器给viewpager
        vpPager.setAdapter(adapter);
            //将tablayout与viewpager相关联
        tabLayout.setupWithViewPager(vpPager);
        //初始化viewpager的信息
        initTab(adapter);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    protected abstract void initTab(BaseAdapter adapter);

public Bundle getBundle(String info){
    Bundle bundle = new Bundle();

    bundle.putString("key",info);
    return bundle;
}

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
